package com.example.doubledatasource.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.doubledatasource.entity.Product;
import com.example.doubledatasource.mapper.ProductMapper;
import com.example.doubledatasource.query.ProductQuery;
import com.example.doubledatasource.service.ProductService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 产品服务实现类 - 使用从数据源
 *
 * @author example
 * @since 1.0.0
 */
@Service
@DS("slave") // 指定使用从数据源
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public List<Product> findByNameLike(String name) {
        return baseMapper.findByNameLike(name);
    }

    @Override
    public List<Product> findProductsInStock() {
        return baseMapper.findProductsInStock();
    }

    @Override
    public IPage<Product> findProductsByQuery(ProductQuery query) {
        // 验证并设置默认值
        query.validate();

        // 构建查询条件
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();

        // 产品名称模糊查询
        if (StringUtils.hasText(query.getName())) {
            queryWrapper.like(Product::getName, query.getName());
        }

        // 产品描述模糊查询
        if (StringUtils.hasText(query.getDescription())) {
            queryWrapper.like(Product::getDescription, query.getDescription());
        }

        // 价格范围查询
        if (query.getMinPrice() != null) {
            queryWrapper.ge(Product::getPrice, query.getMinPrice());
        }
        if (query.getMaxPrice() != null) {
            queryWrapper.le(Product::getPrice, query.getMaxPrice());
        }

        // 库存范围查询
        if (query.getMinStock() != null) {
            queryWrapper.ge(Product::getStock, query.getMinStock());
        }
        if (query.getMaxStock() != null) {
            queryWrapper.le(Product::getStock, query.getMaxStock());
        }

        // 是否有库存
        if (query.getInStock() != null) {
            if (query.getInStock()) {
                queryWrapper.gt(Product::getStock, 0);
            } else {
                queryWrapper.eq(Product::getStock, 0);
            }
        }

        // 排序
        if ("asc".equalsIgnoreCase(query.getSortDir())) {
            addOrderBy(queryWrapper, query.getSortBy(), true);
        } else {
            addOrderBy(queryWrapper, query.getSortBy(), false);
        }

        // 分页查询
        Page<Product> page = new Page<>(query.getPage(), query.getSize());
        return this.page(page, queryWrapper);
    }

    /**
     * 添加排序条件
     */
    private void addOrderBy(LambdaQueryWrapper<Product> queryWrapper, String fieldName, boolean isAsc) {
        switch (fieldName) {
            case "name":
                if (isAsc) {
                    queryWrapper.orderByAsc(Product::getName);
                } else {
                    queryWrapper.orderByDesc(Product::getName);
                }
                break;
            case "price":
                if (isAsc) {
                    queryWrapper.orderByAsc(Product::getPrice);
                } else {
                    queryWrapper.orderByDesc(Product::getPrice);
                }
                break;
            case "stock":
                if (isAsc) {
                    queryWrapper.orderByAsc(Product::getStock);
                } else {
                    queryWrapper.orderByDesc(Product::getStock);
                }
                break;
            case "updateTime":
                if (isAsc) {
                    queryWrapper.orderByAsc(Product::getUpdateTime);
                } else {
                    queryWrapper.orderByDesc(Product::getUpdateTime);
                }
                break;
            case "createTime":
            default:
                if (isAsc) {
                    queryWrapper.orderByAsc(Product::getCreateTime);
                } else {
                    queryWrapper.orderByDesc(Product::getCreateTime);
                }
                break;
        }
    }
}
